import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/pages/login/Login'
import Main from '@/pages/main/Main'

// Main
import Admin from '@/pages/main/views/Admin'
import DataShow from '@/pages/main/views/DataShow'
import Delivery from '@/pages/main/views/Delivery'
import Order from '@/pages/main/views/Order'
import Suggestion from '@/pages/main/views/Suggestion'
import User from '@/pages/main/views/User'
import Goods from '@/pages/main/views/Goods'

Vue.use(Router)
const routes = [
  {
    path: '/',
    component: Login,
    meta:{
      title:'登录'
    }
  },
  {
    path: '/login',
    component: Login,
    meta:{
      title:'登录'
    }
  },
  {
    path: '/main',
    name: 'main',
    component: Main,
    meta:{
      title:'首页',
      // requireAuth: true
    },
    children:[
      // {
      //   path: '',
      //   name:'',
      //   component: Order,
      //   meta:{
      //     title:'订单管理',
      //   }
      // },
      {
        path: 'user',
        name:'user',
        component: User,
        meta: {
          title:'用户管理',
        }
      },
      {
        path: 'order',
        name:'order',
        component: Order,
        meta: {
          title:'订单管理',
        }
      },
      {
        path: 'delivery',
        name:'delivery',
        component: Delivery,
        meta: {
          title:'配送管理',
        }
      },
      {
        path: 'suggestion',
        name: 'suggestion',
        component: Suggestion,
        meta: {
          title:'意见建议',
        }
      },
      {
        path: 'admin',
        name: 'admin',
        component: Admin,
        meta: {
          title:'管理员管理',
        }
      },
      {
        path: 'goods',
        name: 'goods',
        component: Goods,
        meta: {
          title:'商品管理',
        }
      }
    ]
  }
]

const router = new Router({
  mode: 'history',
  // base: '/',
  scrollBehavior (to, from, savedPosition) {
    //判断如果滚动条的位置存在直接返回到当前位置，否者返回到起点
    return savedPosition || { x: 0, y: 0 }
  },
  routes
});

// 路由拦截器
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requireAuth)) {
      // if (store.state.token) {
      //     next();
      // }
      let isValid = sessionStorage.getItem('isValid');
      if (isValid == 'true') {
          next();
      }else {
          next({
              path: '/login',
              query: {redirect: to.fullPath}
          })
      }
  }else {
      next();
  }
})

export default router;
